package com.search.config;

import com.search.FxApplication;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;

import java.net.URL;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

@Configuration
@ConfigurationProperties("fxml")
public class FxInjectConfig {


    private Map<String,String> views; //fxml文件路径
    private int width; //窗体宽度
    private int height; //窗体高度
    private String title; //窗体
    private String icon; //窗体左上角图标
    private boolean resizable;//是否可以改变窗口大小
    private  String mainView;    //主视图
    private  String splashGif;    //启动动画
    private static  Stage stage; //主窗体


    public Map<String, String> getViews() {
        return views;
    }

    public void setViews(Map<String, String> views) {
        this.views = views;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public void setResizable(boolean resizable) {
        this.resizable = resizable;
    }

    public static Stage getStage() {
        return stage;
    }

    public void setMainView(String mainView) {
        this.mainView = mainView;
    }

    public void setSplashGif(String splashGif) {
        this.splashGif = splashGif;
    }

    /**
     *
     * @param stage 窗口对象
     * @param init 窗口初始化回调,在展示窗口之前调用
     * @throws Exception
     */
    public  void initStage(Stage stage, Consumer<Stage> init) throws Exception {
        ConfigurableApplicationContext applicationContext = FxApplication.applicationContext;
        Set<Map.Entry<String, String>> entries = views.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            URL resource = getClass().getResource(entry.getValue());
            if (resource == null) {
                throw new Exception( entry.getValue()+"文件不存在");
            }
             FXMLLoader loader = new FXMLLoader(resource);
            loader.setControllerFactory(new Callback<Class<?>, Object>() {
                @Override
                public Object call(Class<?> param) {
                    // 控制器工厂提供bean注入
                    return applicationContext.getBean(param);
                }
            });
            Object load = loader.load();
            Pane pane = (Pane) load;
            double prefWidth = pane.getPrefWidth();
            double prefHeight = pane.getPrefHeight();
            Scene scene = new Scene((Parent) load,prefWidth,prefHeight);
            SpringBeanFactory.setBean(entry.getKey(),scene);
        }
        Scene sceneMain = applicationContext.getBean(mainView,Scene.class);
        stage.setScene(sceneMain);
        //自动适配大小,如果场地比窗体大,则窗体大小为场地大小,否则窗体大小为配置大小
        stage.setWidth(width<sceneMain.getWidth()?sceneMain.getWidth():width);
        stage.setHeight(height<sceneMain.getHeight()?sceneMain.getHeight():height);
        stage.setTitle(title);
        URL resource = getClass().getResource(icon);
        String externalForm = resource.toExternalForm();
        stage.getIcons().add(new javafx.scene.image.Image(externalForm));
        stage.setResizable(resizable);
        stage.centerOnScreen();
        FxInjectConfig.stage = stage;
        if(init!=null){
            init.accept(stage);
        }

        StartLoadFroms loadForms = new StartLoadFroms(width,height,splashGif,stage);
        loadForms.show();//启动窗体(启动窗体(动画)+检测主窗体加载+启动主窗体,关闭启动窗体)
    }

    public  static void   cutView(String viewName){
        Scene bean = FxApplication.applicationContext.getBean(viewName, Scene.class);
        stage.setScene(bean);
        stage.show();
    }



}
